﻿package net.wangit.view.unit.sum;

import java.util.List;
import java.util.Map;

import net.wangit.Element;
import net.wangit.MapList;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitComponent;


public class SumProcessor {

	private static final Map BLANK_ROW = null;

	public SumProcessor() {
	}

	public static void sum(ActionContext ac, UnitComponent uc, Element select,
			Element index) {
		if (Checker.isEmpty(uc.getSumConfig()))
			return;
		MapList data = uc.getData();
		if (Checker.isEmpty(data))
			return;
		try {
			SumConfig sumConfig = new SumConfig(ac, uc);
			data = uc.getData();
			List rows = sumConfig.getSumRows(data.size() - 1, uc);
			for (int i = 0; i < rows.size(); i++) {
				SumRow row = (SumRow) rows.get(i);
				int rowIndex = row.getRowIndex();
				data.insertRow(rowIndex, BLANK_ROW);
				row.setTexts(uc, rowIndex, data.size() - 1, select, index);
			}

		} catch (Exception e) {
			throw new SumConfigurationException(uc.getDomain(), uc.getId(), e);
		}
	}

}
